package org.mark.user.controller;

import org.mark.common.response.Result;
import org.mark.user.entity.User;
import org.mark.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }

    /**
     * 分页查询用户
     */
    @GetMapping("/page")
    public Result getPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String search) {
        return userService.getPage(pageNum, pageSize, search);
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/update")  // 改为 POST 请求
    public Result update(@RequestBody User user) {
        return userService.update(user);
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")  // 改为 POST 请求
    public Result delete(@RequestBody Map<String, String> params) {
        String userId = params.get("userId");
        return userService.delete(userId);
    }
}